package com.googlecode.jsitemap;

import java.io.UnsupportedEncodingException;

/**
 * @author Denis Migol
 * 
 */
public abstract class BuilderBase<E extends EntryBase<?>, P extends BuildParamsBase<?>> implements Builder<E, P> {
	protected static final String LINE_SEPARATOR = "\n";

	protected P params;

	public BuilderBase(final P params) {
		this.params = params;
	}

	protected static byte[] getBytes(final String s) {
		try {
			return s.getBytes(Util.UTF8);
		} catch (final UnsupportedEncodingException e) {
			return s.getBytes();
		}
	}

	protected byte[] newLine() {
		return getBytes(LINE_SEPARATOR);
	}

	protected boolean canWrite(final int bytesWritten, final int bytesToWrite) {
		return (bytesWritten + bytesToWrite) <= params.getMaxSize();
	}

	protected abstract byte[] buildEntry(E entry);
}
